package com.example.haha_.ui_demo.recyclerview;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.example.haha_.ui_demo.R;
import com.example.haha_.ui_demo.beans.Shop;
import com.example.haha_.ui_demo.retrofitservice.Retrofitservice;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class RecyclerViewAdapter
            extends RecyclerView.Adapter<ItemViewHolder> {
        private List<DataItem> dataItemList;
        public void setData(List<DataItem> dataItemList) {
            this.dataItemList = dataItemList;
            notifyDataSetChanged();
        }
        @NonNull
        @Override
        public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            //获取LayoutInflater对象，它可以根据布局定义的内容⽣成⼀个View对象
            LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
            //⽣成View对象，
            View itemView = layoutInflater.inflate(R.layout.item_layout, viewGroup, false);
            //实例化⼀个ViewHolder
            ItemViewHolder viewHolder = new ItemViewHolder(itemView);
            //返回ViewHolder
            return viewHolder;
        }
        @Override
        public void onBindViewHolder(@NonNull ItemViewHolder itemViewHolder, int i) {
            //说明： i是数据项的位置，决定了显示的次序， i==0，就是第⼀条
            //获取第i条数据
//            final DataItem dataItem = dataItemList.get(i);
//            //将第i条数据绑定到列表项（View）中。 ImageView未做处理，后⾯课程会介绍
//
//            itemViewHolder.textView1.setText(dataItem.getText1());
//            itemViewHolder.textView2.setText(dataItem.getText2());
//            load(i);
            String s[] = {"1", "2", "3", "5", "6"};
            Retrofit retrofit =
                    new Retrofit.Builder()
                            .baseUrl("http://172.24.10.175:8080/foodService/")
                            .build();

            Retrofitservice service =
                    retrofit.create(Retrofitservice.class);

            Call<ResponseBody> shopCall =
                    service.getShopById(s[i]);

            shopCall.enqueue(new Callback<ResponseBody>() {

                @Override
                public void onResponse(Call<ResponseBody> call,
                                       Response<ResponseBody> response) {

                    try {

                        byte[] bytes = response.body().bytes();
                        String text = new String(bytes);

                        Gson gson = new Gson();

                        Shop shop = gson.fromJson(text, Shop.class);
                        DataItem dataItem = new DataItem();
                        String shopName = shop.getShopname();
                        String pic = shop.getPic();
//                    str="";
//                    str+=shopName;

                        itemViewHolder.textView1.setText(shopName);
                        dataItem.setText1("text1:" + shopName);
                        String picUrl =
                                "http://172.24.10.175:8080/foodService" + pic;

                        Picasso.get()
                                .load(picUrl)
                                .into(itemViewHolder.imageView);
//                    str+="---";
//                    str+=picUrl;
//                    System.out.println("/////////"+str+"////////");

                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {

//                Toast.makeText(ItemViewHolder.this,
//                        t.getLocalizedMessage(),
//                        Toast.LENGTH_SHORT).show();
                }
            });
//            data.add(dataItem);
        }
        @Override
        public int getItemCount() {
            return dataItemList.size();
        }

    }

